const {
  uniacid
} = require("../../siteinfo");

const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: {},
    bottomlist: [
      // {
      //   text: "会员中心",
      //   image: "../../images/mine-vip.png",
      //   url: "pages/vip/vippurches/index"
      // },
      // {
      //   text: "分销中心",
      //   image: "../../images/mine-fenxiao.png",
      //   url: "pages/dealer/index/index"
      // },
      {
        text: "优惠券",
        image: "../../images/mine-quan.png",
        url: "pages/user/coupon/coupon"
      },
      {
        text: "我的砍价",
        image: "../../images/mine-kanjia.png",
        url: "pages/bargain/index/index?tab=1"
      },
      {
        text: "收货地址",
        image: "../../images/mine-address.png",
        url: "pages/address/index"
      }, {
        text: "领券中心",
        image: "../../images/mine-lingquan.png",
        url: "pages/coupon/coupon"
      },

    ],
    isLogin: false,
    userInfo: {}, // 用户信息
    orderCount: {}, // 订单数量
    baseurl: App.baseurl,
    maskshow: false,
    checked: true,
    birth: "选择生日"
  },
  /**
   * 获取我的团队信息
   */
  getteaminfo() {
    let _this = this;
    App._post_form('user.team/getMyInfoByTeam', {}, (result) => {
      this.setData({
        is_has_team: result.data.is_has_team
      })

    });
  },
  handleBirth(e) {
    wx.showModal({
      title: '友情提示',
      content: '请谨慎选择，只可选择一次，选择后不可修改',
      success:(res)=> {

        if (res.confirm) {
          console.log(e.detail.value)
          App._post_form('user/updateBirthday', {
            birthday: e.detail.value
          }, (result) => {
            this.getUserDetail()

          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }

      }
    });

  },
  //点击选中协议
  handleCheck() {
    this.setData({
      checked: !this.data.checked
    })
  },
  //打开广告协议
  handlemaskshow() {
    this.setData({
      maskshow: true
    })
  },

  //关闭广告协议
  handlemaskClose() {
    this.setData({
      maskshow: false
    })
  },
  //点击分享
  handleShare(e) {
    if (!this.data.checked) {
      App.showError("请先选择协议")
    }
    console.log(e)
  },
  // 开通会员
  handleopen() {
    wx.navigateTo({
      url: '../vip/vipcenter/index',
    })
  },

  // 
  jump(){
    wx.navigateTo({
      url: '/pages/dealer/index/index',
    })
  },
  //获取手机号
  handlePhone(e) {
    let _this = this
    if (!_this.onCheckLogin()) {
      return false;
    }
    console.log(e)
    //获取手机号
    wx.login({
      success: (res) => {

        App._post_form("user/bindPhone", {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          code: res.code
        }, () => {
          wx.showToast({
            title: '绑定成功',
          })
          this.getUserDetail();

        })
      }
    })

  },
  //点击签到
  handleSign() {
    let _this = this
    if (!_this.onCheckLogin()) {
      return false;
    }

    wx.navigateTo({
      url: '../sign/money/index',
    })
  },
  // 获取广告
  fetchAdver() {
    App._post_form('info/getAdverts', {
      type: 1
    }, (res) => {
      console.log(res.data)
      this.setData({
        adverts: res.data.adverts
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchAdver()
    App.editTabbar();
    if (wx.getStorageSync('token')) {
      this.getteaminfo()
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //隐藏系统tabbar
    wx.hideTabBar();
    let _this = this;
    _this.setData({
      isLogin: App.checkIsLogin()
    });
    // 获取当前用户信息
    _this.getUserDetail();
    // 更新购物车角标
    App.setCartTabBadge()
  },

  /**
   * 获取当前用户信息
   */
  getUserDetail() {
    let _this = this;
    App._get('user.index/detail', {}, function (result) {
      _this.setData(result.data);
      wx.setStorageSync('points', result.data.userInfo.points)
    });
  },

  /**
   * 订单导航跳转
   */
  onTargetOrder(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    let urls = {
      all: '/pages/order/index?type=all',
      payment: '/pages/order/index?type=payment',
      received: '/pages/order/index?type=received',
      refund: '/pages/order/refund/index',
    };
    // 转跳指定的页面
    wx.navigateTo({
      url: urls[e.currentTarget.dataset.type]
    })
  },

  /**
   * 菜单列表导航跳转
   */
  onTargetMenus(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    wx.navigateTo({
      url: '/' + e.currentTarget.dataset.url
    })
  },

  /**
   * 跳转我的钱包页面
   */
  onTargetWallet(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    wx.navigateTo({
      url: './wallet/index'
    })
  },

  /**
   * 跳转积分明细页
   */
  onTargetPoints(e) {

    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    wx.navigateTo({
      url: '../dealer/index/index'
    });
    // ../points/log/index  积分路径
  },
  /**
   * 跳转到我的收益
   */
  onTargetMoney() {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    wx.navigateTo({
      url: './money/index/index'
    });

  },

  /**
   * 跳转到我的提现
   */
  onTargetTixian() {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    if (this.data.is_has_team === 1) {
      wx.navigateTo({
        url: '../family/home/index',
      })
      return false
    }
    wx.navigateTo({
      url: '../family/index'
    });

  },

  /**
   * 跳转到登录页
   */
  onLogin() {
    // wx.navigateTo({
    //   url: '../login/login',
    // });
    App.doLogin();
  },

  /**
   * 验证是否已登录
   */
  onCheckLogin() {
    let _this = this;
    if (!_this.data.isLogin) {
      App.showError('很抱歉，您还没有登录');
      return false;
    }
    return true;
  },

})